package com.trailwalker.javafx.utils.image;

import com.trailwalker.JavaUtils.string.StringUtils;
import com.trailwalker.javafx.utils.resource.ResourceUtils;
import com.trailwalker.javafx.utils.style.AdaptUtils;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.chart.StackedAreaChart;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @author Trailwalker
 * @create 2020-07-12 18:02
 */
public class ImageUtils {

    public static Image getLocalImage(File file) {
        if (file != null && file.exists() && file.isFile()) {
            return getLocalImage(file.getAbsolutePath());
        } else {
            return null;
        }
    }

    public static Image getLocalImage(String filePath) {
        Image image = new Image("file:" + filePath, true);
        return image;
    }

    /**
     * 获取指定字符串的宽度
     *
     * @param content  指定的字符串
     * @param fontSize 字符串的字体大小
     * @param fontName 字符串的字体名称
     * @return 返回指定字符串的宽度
     */
    public static int getStringWidth(String content, String fontName, int fontSize) {
        BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics = bufferedImage.createGraphics();
        FontMetrics fontMetrics = graphics.getFontMetrics(new Font(fontName, Font.PLAIN, fontSize));
        return fontMetrics.stringWidth(content);
    }

    public static ImageView getImageView(Object object, double scaleSize) {
        return getImageView(object, scaleSize, scaleSize, null, null);
    }

    public static ImageView getImageView(Object object, double scaleSize, String imageResourcePath, String hoverImageResourcePath) {
        return getImageView(object, scaleSize, scaleSize, imageResourcePath, hoverImageResourcePath);
    }

    public static ImageView getImageView(Object object, double scaleWidth, double scaleHeight, String imageResourcePath, String hoverImageResourcePath) {
        AdaptUtils adaptUtils = AdaptUtils.getInstance();
        ImageView imageView = new ImageView();
        imageView.setFitWidth(adaptUtils.getScale(scaleWidth));
        imageView.setFitHeight(adaptUtils.getScale(scaleHeight));
        imageView.setPickOnBounds(true);
        if (StringUtils.isValid(imageResourcePath)) {
            imageView.setImage(new Image(ResourceUtils.getImage(object, imageResourcePath)));
            if (StringUtils.isValid(hoverImageResourcePath)) {
                imageView.hoverProperty().addListener(new ChangeListener<Boolean>() {
                    @Override
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                        if (newValue) {
                            imageView.setImage(new Image(ResourceUtils.getImage(object, hoverImageResourcePath)));
                        } else {
                            imageView.setImage(new Image(ResourceUtils.getImage(object, imageResourcePath)));
                        }
                    }
                });
            }
        }
        return imageView;
    }

    public static Image getBottomRadiusImage(Image image, int radius) {
        BufferedImage fxBi = SwingFXUtils.fromFXImage(image, null);
        BufferedImage bufferedImage = new BufferedImage((int) image.getWidth(), (int) image.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D gs = bufferedImage.createGraphics();

        gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gs.setClip(new RoundRectangle2D.Double(0, -radius, (int) image.getWidth(), (int) image.getHeight() + radius, radius, radius));
        gs.drawImage(fxBi, 0, 0, null);
        gs.dispose();
        return SwingFXUtils.toFXImage(bufferedImage, null);
    }

}
